package com.usdt.system.cp.utils;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.usdt.common.exception.ServiceException;
import com.usdt.system.service.cp.RequestManageService;

import jakarta.servlet.http.HttpServletRequest;

@Component
public class UserAndDeptUtils {
	
	//public static final ThreadLocal<Map<String, String>> threadLocalMessage = new ThreadLocal<Map<String, String>>();
	
	public static Map<String, String> getCurrentUserIdAndDeptId() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
				.getRequest();
		
		String currentUserId =(String)request.getAttribute("currentUserId");
		String currentDeptId =(String)request.getAttribute("currentDeptId");
		String userName =(String)request.getAttribute("userName");
		if(StringUtils.isBlank(currentUserId) || StringUtils.isBlank(currentDeptId)) {
			throw new ServiceException("用户id和部门不能为空", -1);
		}
		Map<String, String> map = new HashMap<>();
		map.put("currentUserId", currentUserId);
		map.put("currentDeptId", currentDeptId);
		map.put("userName", userName);
		return map;
	}
	public static String getCurrentUserId() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
				.getRequest();
		
		String currentUserId =(String)request.getAttribute("currentUserId");
		String currentDeptId =(String)request.getAttribute("currentDeptId");
		if(StringUtils.isBlank(currentUserId) || StringUtils.isBlank(currentDeptId)) {
			throw new ServiceException("用户id不能为空", -1);
		}
		return currentUserId;
	}
	public static String getCurrentDeptId() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
				.getRequest();
		
		String currentUserId =(String)request.getAttribute("currentUserId");
		String currentDeptId =(String)request.getAttribute("currentDeptId");
		if(StringUtils.isBlank(currentUserId) || StringUtils.isBlank(currentDeptId)) {
			throw new ServiceException("用户部门不能为空", -1);
		}
		return currentDeptId;
	}
}
